package com.ls.hnks.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.ls.hnks.util.Result;
import com.ls.hnks.model.Training;
import com.ls.hnks.service.TrainingService;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@SaCheckLogin
@RestController
@RequestMapping("/train")
public class TrainingController {

    @Autowired
    private TrainingService trainingService;

    @RequestMapping("/list")
    public Result list(@ApiParam("分页查询的页码数") @RequestParam(value = "page",defaultValue = "1")Integer page,
                       @ApiParam("分页查询的页大小") @RequestParam(value = "size",defaultValue = "20")Integer size){
        return trainingService.getTrainingByPage(page,size);
    }

    @PostMapping("/save")
    public Result save(@RequestBody Training training){
        log.info("培训信息 --- {}", JSON.toJSONString(training));
        trainingService.saveTrain(training);
        return Result.ok();
    }

    @RequestMapping("/details/{id}")
    public Result getDetails(@PathVariable String id){
        return trainingService.getDetails(id);
    }

    @PostMapping("/finish")
    public Result finish(String trainId){
        String selfId = StpUtil.getLoginId().toString();
        return trainingService.finishTrain(selfId,trainId);
    }
}
